import sys
from PyQt5.QtWidgets import (
    QApplication,
    QWidget,
    QLabel,
    QListWidgetItem,
    QHBoxLayout,
    QVBoxLayout,
    QPushButton,
    QDialog,
)
from gleaner.projector import Projector
from gleaner.recorder_dialog import RecorderDialog
from gleaner.register import is_admin, add_desktop_context_menu, ctypes
from gleaner.utils import load_json, relative
from gleaner.dropin_list import DropInList
from gleaner.log_dialog import LogDialog
import argparse


# 这一个函数的位置不能随便移动
def register(command):
    if is_admin():
        add_desktop_context_menu(command)
    else:
        if sys.argv[0].endswith(".exe"):
            program = sys.argv[0]
            argument = " ".join(['--register "' + command + '"'])
        else:
            program = sys.executable
            argument = " ".join([__file__, '--register "' + command + '"'])
        # Re-run the program with admin rights
        ctypes.windll.shell32.ShellExecuteW(
            None, "runas", program, argument, None, 1,
        )


class Demo(QWidget):
    def __init__(self):
        super(Demo, self).__init__()
        config_file = relative("data/config.json")
        config = load_json(config_file)
        extension_file = relative(config["extensions"])
        default_extensions = load_json(extension_file)

        h_layout = QHBoxLayout()
        self.lists = dict()
        for i, (key, values) in enumerate(default_extensions.items()):
            list_widget = DropInList(self)
            box = QVBoxLayout()
            btn = QLabel(key)
            for val in values:
                list_widget.addItem(QListWidgetItem(val))
            box.addWidget(btn)
            box.addWidget(list_widget)
            h_layout.addLayout(box)
            self.lists[key] = list_widget

        self.v_layout = QVBoxLayout()

        h_layout2 = QHBoxLayout()
        preview_btn = QPushButton("preview")

        undo_btn = QPushButton("undo")
        exit_btn = QPushButton("exit")
        reg_btn = QPushButton("register")
        h_layout2.addWidget(preview_btn)

        h_layout2.addWidget(undo_btn)
        h_layout2.addWidget(reg_btn)
        h_layout2.addWidget(exit_btn)

        self.v_layout.addLayout(h_layout)
        self.v_layout.addLayout(h_layout2)

        self.setLayout(self.v_layout)

        preview_btn.clicked.connect(self.preview)
        undo_btn.clicked.connect(self.undo)
        exit_btn.clicked.connect(lambda: exit(0))
        reg_btn.clicked.connect(lambda: register(sys.argv[0]))

        self.recorder = None

    def get_extensions(self):
        return {
            key: widget.get_items() for key, widget in self.lists.items()
        }

    def preview(self):
        extensions = self.get_extensions()
        projector = Projector(extensions)
        recorder = projector.project()
        dialog = RecorderDialog(recorder)
        if dialog.exec_() == QDialog.Accepted:
            pass
        self.recorder = recorder

    def undo(self):
        the_dialog = LogDialog()
        if the_dialog.exec_() == QDialog.Accepted:
            pass


if __name__ == "__main__":
    parser = argparse.ArgumentParser()

    parser.add_argument("--register", type=str, default=None, help="[windows] Register to context menu of desktop")
    args = parser.parse_args()

    if args.register is not None:
        register(args.register)
        exit(0)

    # import qtmodern.styles
    # import qtmodern.windows

    app = QApplication(sys.argv)
    demo = Demo()
    # # qtmodern.styles.dark(app)
    # mw = qtmodern.windows.ModernWindow(demo)
    # mw.show()
    demo.show()
    sys.exit(app.exec_())
